const cheerio = require('cheerio')
const axios = require('axios')

module.exports = {
    async urlParse(ctx, next) {
        let { url } = ctx.request.body;
        try{
            const res = await axios({
                url:url,
                method:'get'
            })
            if (!res) {
                ctx.body = {
                    code: 200,
                    msg: '未获取到标题!',
                    data: {
                        url: url,
                        title: '未获取到标题',
                        thumb: '',
                    }
                  }
            }
            const html_str = res.data.toString();
            const $ = cheerio.load(html_str);
            const title = $('title').text(); //日
            ctx.body = {
                code: 200,
                msg: '获取成功',
                data: {
                    url: url,
                    title: title,
                    thumb: '',
                }
            }
        }catch(e){
            ctx.body = {
                code: 200,
                msg: '获取成功',
                data: {
                    url: url,
                    title: '未获取到标题',
                    thumb: '',
                }
              }
        }
    }
}